package com.atguigu.eduservice.controller;


import com.atguigu.commonutils.R;
import com.atguigu.eduservice.entity.EduTeacher;
import com.atguigu.eduservice.entity.vo.TeacherQuery;
import com.atguigu.eduservice.service.EduTeacherService;
import com.atguigu.servicebase.Myexception.GuliException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author lhl
 * @since 2022-06-29
 */
@RestController
@RequestMapping("/eduservice/edu-teacher")
@CrossOrigin
public class EduTeacherController {
    @Autowired
    EduTeacherService eduservice;
    @GetMapping("/findAll")
    public R findAll(){
        System.out.println("进来了吗");
        List<EduTeacher> list = eduservice.list(null);

        return R.ok().data("items",list);
    }
    @DeleteMapping("{id}")
    public R removeById(@PathVariable String id){
        if(eduservice.removeById(id)){
            return R.ok();
        }else{
            return R.error();
        }
    }
//    分页查询
    @GetMapping("{page}/{limit}")
    public R pageList(@PathVariable Long page,@PathVariable Long limit){
        Page<EduTeacher> eduTeacherPage = new Page<>(page, limit);
        eduservice.page(eduTeacherPage,null);
//        链式调用
        return R.ok().data("total",eduTeacherPage.getTotal()).data("rows",eduTeacherPage.getRecords());
    }
//    多条件分页查询
    @PostMapping("pageTeacherCondition/{page}/{limit}")
    public R pageTeacherCondition(@PathVariable Long page,@PathVariable Long limit,
    @RequestBody TeacherQuery teacherQuery){
//        创建查询包装器
        QueryWrapper<EduTeacher> teacherQueryQueryWrapper = new QueryWrapper<>();
        if(!StringUtils.isEmpty(teacherQuery.getName())){
            teacherQueryQueryWrapper.like("name",teacherQuery.getName());
        }
        if(!StringUtils.isEmpty(teacherQuery.getLevel())){
            teacherQueryQueryWrapper.like("level",teacherQuery.getLevel());
        }
        if(!StringUtils.isEmpty(teacherQuery.getBegin())){
//            大于等于
            teacherQueryQueryWrapper.ge("gmt_create",teacherQuery.getBegin());
        }
        if(!StringUtils.isEmpty(teacherQuery.getEnd())){
//            小于等于
            teacherQueryQueryWrapper.le("gmt_modified",teacherQuery.getEnd());
        }
//        按照添加时间降序排序
        teacherQueryQueryWrapper.orderByDesc("gmt_create");
        Page<EduTeacher> eduTeacherPage = new Page<>(page, limit);
        eduservice.page(eduTeacherPage,teacherQueryQueryWrapper);
        return R.ok().data("total",eduTeacherPage.getTotal()).data("rows",eduTeacherPage.getRecords());
    }
//    新增讲师
    @PostMapping("addTeacher")
    public R save(@RequestBody EduTeacher eduTeacher){
        boolean save = eduservice.save(eduTeacher);
        if(save){
            return R.ok();
        }else{
            return R.error();
        }

    }
//    查询讲师
    @GetMapping("/getTeacher/{id}")
    public R getTeacherByid(@PathVariable String id){
//        try{
//            int a = 1/0;
//        }catch (Exception e){
//            throw new GuliException(200001,"自定义异常");
//        }

        EduTeacher byId = eduservice.getById(id);
        if(byId != null){
            return R.ok().data("teacher",byId);
        }else{
            return R.error();
        }
    }
//    修改讲师
    @PostMapping("/updateTeacher")
    public R updateTeacher(@RequestBody EduTeacher eduTeacher){
        boolean b = eduservice.updateById(eduTeacher);
        if (b){
            return R.ok();
        }else{
            return R.error();
        }
    }
}

